﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IosDatamodel;
using System.IO;
using IOSpro.Lib;
using IOSpro.Models;
namespace IOSpro.Controllers
{
    public class VideoSheetController : Controller
    {
        //
        // GET: /VideoSheet/


        private IosDBEntities db;
        private IVideosheetBL ibl;

        public VideoSheetController() 
        {
            db = new IosDBEntities();
            ibl = new VideosheetBL();
          
        }



        public ActionResult Index()
        {
            var aa = from a in db.VideoSheet select a;
            return View(aa);
        }

        //
        // GET: /VideoSheet/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /VideoSheet/Create

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create( VideosheetAdd Model)
        {
            ibl.SaveVideosheetname(Model);
            return View();
        }    
        
        
        //
        // POST: /VideoSheet/Create

        //
        // GET: /VideoSheet/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /VideoSheet/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddImage(HttpPostedFileBase fileData, int videoid)
        {
            if (fileData != null)
            {
                try
                {

                    string oldfilePath = "~/Uploads/videosheet/" + fileData.FileName;
                    if (System.IO.File.Exists(Server.MapPath(oldfilePath)))//判断文件是否存在
                    {
                        System.IO.File.Delete(Server.MapPath(oldfilePath));
                    }


                    // 文件上传后的保存路径
                    //      int questionsetid1 = Convert.ToInt32(questionsetid);
                    string filePath = Server.MapPath("~/Uploads/videosheet/");
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    string fileName = Path.GetFileName(fileData.FileName);// 原始文件名称
                    string fileExtension = Path.GetExtension(fileName); // 文件扩展名
                    string saveName = fileName;// 保存文件名称
                    //  if()
                    var path = Path.Combine(filePath, saveName);
            //        if (System.IO.File.Exists(path))
             //       {
              //          System.IO.File.Delete(Server.MapPath(path));
               //     }
                    fileData.SaveAs(path);
                    ibl.SaveVideosheetimage(videoid, fileName);
                    return Json(new { Success = true, FileName = fileName, SaveName = saveName });
                }
                catch (Exception ex)
                {
                    return Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {

                return Json(new { Success = false, Message = "请选择要上传的文件！" }, JsonRequestBehavior.AllowGet);
            }
        }



        //
        // GET: /VideoSheet/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /VideoSheet/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
